﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace DtoFw.Enum
{
    /// <summary>
    /// HTTP 方法
    /// </summary>
    public enum EnumHttpMethod
    {
        /// <summary>
        /// Http Get
        /// </summary>
        Get,

        /// <summary>
        /// Http Post
        /// </summary>
        Post,

        /// <summary>
        /// Http Put
        /// </summary>
        Put,

        /// <summary>
        /// Http Delete
        /// </summary>
        Delete,

        /// <summary>
        /// Http Head
        /// </summary>
        Head,

        /// <summary>
        /// Http Options
        /// </summary>
        Options,

        /// <summary>
        /// Http Trace
        /// </summary>
        Trace,
    }


    /// <summary>
    /// token类型
    /// </summary>
    public enum EnumTokenType
    {
        AccessToken = 1,
        RefreshToken = 2
    }

    /// <summary>
    /// Ftp文件类型目录
    /// </summary>
    public enum FtpDir
    {
        img,
        excle,
        pdf,
        other,
        video,
        word
    }

    /// <summary>
    /// 平台
    /// </summary>
    public enum EnumChannel
    {
        [Description("未知")]
        None = 0,

        /// <summary>
        /// 网站
        /// </summary>
        [Description("网站应用")]
        Site = 1,

        /// <summary>
        /// 手机网站
        /// </summary>
        [Description("手机网站")]
        Mobile = 2,

        /// <summary>
        /// 苹果
        /// </summary>
        [Description("苹果应用")]
        IOS = 3,

        /// <summary>
        /// 安卓
        /// </summary>
        [Description("安卓应用")]
        Android = 4,

        /// <summary>
        /// 小程序
        /// </summary>
        [Description("小程序应用")]
        Applit = 5,

        /// <summary>
        /// 微信平台应用
        /// </summary>
        [Description("微信平台应用")]
        WeChatPlatform = 6,

        /// <summary>
        /// 微信企业平台应用
        /// </summary>
        [Description("微信企业平台应用")]
        WeChatWorkPlatform = 7,

        /// <summary>
        /// 系统
        /// </summary>
        [Description("系统平台")]
        Sys = 9,
    }

    /// <summary>
    /// 数据库
    /// </summary>
    public enum EnumDatabase
    {
        /// <summary>
        /// 开放平台
        /// </summary>
        Mysql,

        /// <summary>
        /// 联创版mysql
        /// </summary>
        LianChuangMysql,

        /// <summary>
        /// 初中
        /// </summary>
        MongoDB,
        /// <summary>
        /// sqlserver
        /// </summary>
        SqlServer
    }

    /// <summary>
    /// 错误类型
    /// </summary>
    public enum EnumReturnCode
    {
        /// <summary>
        /// 正常
        /// </summary>
        [Description("成功")]
        Success = 200,

        /// <summary>
        /// 应用错误
        /// </summary>
        [Description("系统错误")]
        SysError = 10000,

        /// <summary>
        /// 应用错误
        /// </summary>
        [Description("应用错误")]
        AppError = 20000,

        /// <summary>
        /// 业务错误
        /// </summary>
        [Description("业务错误")]
        BusError = 30000,

        /// <summary>
        /// 业务错误
        /// </summary>
        [Description("业务错误")]
        AppcationError = 500
    }
    /// <summary>
    /// 租户状态
    /// </summary>
    public enum EnumTenantStatus
    {
        /// <summary>
        /// 登录
        /// </summary>
        Login = 10,

        /// <summary>
        /// 注销
        /// </summary>
        Logout = 20
    }
    /// <summary>
    /// 令牌来源
    /// </summary>
    public enum EnumTokenSource
    {
        /// <summary>
        /// 应用令牌
        /// </summary>
        [Description("应用令牌")]
        AppToken = 1,

        /// <summary>
        /// 租户令牌
        /// </summary>
        [Description("租户令牌")]
        TenantToken = 2
    }
    /// <summary>
    /// 错误级别
    /// </summary>
    public enum EnumLogLevel
    {
        /// <summary>
        /// 错误
        /// </summary>
        Error = 10,

        /// <summary>
        /// 警告
        /// </summary>
        Warn = 20,

        /// <summary>
        /// 调试
        /// </summary>
        Debug = 30,

        /// <summary>
        /// 信息
        /// </summary>
        Info = 40
    }
    /// <summary>
    /// 日志业务分类
    /// </summary>
    public enum EnumLogServiceType
    {
        /// <summary>
        /// 应用日志
        /// </summary>
        App = 10,

        /// <summary>
        /// 租户日志
        /// </summary>
        Tenant = 20,

        /// <summary>
        /// 令牌日志
        /// </summary>
        Ticket = 30
    }
    /// <summary>
    /// 区划
    /// </summary>
    public enum EnumRegionLevel
    {
        /// <summary>
        /// 国家
        /// </summary>
        Country = 0,
        /// <summary>
        /// 省
        /// </summary>
        Province = 1,
        /// <summary>
        /// 市
        /// </summary>
        City = 2,
        /// <summary>
        /// 区
        /// </summary>
        Area = 3,
        /// <summary>
        /// 区
        /// </summary>
        Street = 4
    }
    /// <summary>
    /// 枚举对象
    /// </summary>
    public class EnumberEntity
    {
        /// <summary>
        /// 枚举的描述
        /// </summary>
        public string Desction { set; get; }

        /// <summary>
        /// 枚举名称
        /// </summary>
        public string EnumName { set; get; }

        /// <summary>
        /// 枚举对象的值
        /// </summary>
        public int EnumValue { set; get; }
    }
    public static class AppEnum
    {

        private static System.Collections.SortedList GetStatus(System.Type t)
        {
            System.Collections.SortedList list = new System.Collections.SortedList();

            Array a = System.Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                string enumDescription = GetDescription(t, enumKey);
                list.Add(enumKey, enumDescription);
            }
            return list;
        }
        private static string GetName(System.Type t, object v)
        {
            try
            {
                return System.Enum.GetName(t, v);
            }
            catch (Exception ex)
            {
                return "未知";
            }
        }
        public static string GetDescription(System.Type t, object v)
        {
            try
            {
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch (Exception ex)
            {
                return "未知";
            }
        }
        public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
        {
            if (fieldInfo != null)
                return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return null;
        }
        /// <summary>
        /// 根据Description获取枚举
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="description">枚举描述</param>
        /// <returns>枚举</returns>
        public static T GetEnumName<T>(string description, string type)
        {
            Type _type = typeof(T);
            foreach (FieldInfo field in _type.GetFields())
            {
                DescriptionAttribute[] _curDesc = GetDescriptAttr(field);
                if (_curDesc != null && _curDesc.Length > 0)
                {
                    if (_curDesc[0].Description == description)
                        return (T)field.GetValue(null);
                }
                else
                {
                    if (field.Name == description)
                        return (T)field.GetValue(null);
                }
            }
            throw new Exception("未能找到对应的" + type + "：" + description);
        }
        /// <summary>
        /// 枚举转为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumberEntity> EnumToList<T>()
        {
            List<EnumberEntity> list = new List<EnumberEntity>();
            foreach (var e in System.Enum.GetValues(typeof(T)))
            {
                EnumberEntity m = new EnumberEntity();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    if (da != null)
                        m.Desction = da.Description;
                }
                m.EnumValue = Convert.ToInt32(e);
                m.EnumName = e.ToString();
                list.Add(m);
            }
            return list;
        }
    }
}
